<template>
  <div o-Container>
    <o-list v-bind:source="list.space"
            v-bind="{created:create,exported:output,modified:modify,searched:search}"/>
    <o-dialog v-bind:source="panel" v-bind="{opened:load,closed:load}">
      <template slot="content">
        <o-item v-bind:source="item.space"
                v-bind="{saved:save,backed:back,changed:change,validated: validate}"></o-item>
      </template>
    </o-dialog>
  </div>
</template>

<script>
  export default {
    name: "mms_platform_user_list", /* 这个name暂时不知道用啥用，根据官方文档说的是方便排错的 */
    data() {
      return {
        octopus: this.$parent.octopus,
        list: {
          space: {
            data: {
              id: 1281, // 菜单表的八爪鱼ID
              cellData: [
                {name: "ID", searchKey: "=", width: 60},
                {name: "TypeID", filterKey: "<>", filterValue: 1},// 只加载不等于1的语言。因为1是八爪鱼框架维护。
                {name: "Code", searchKey: "LIKE", width: 200},
                {name: "CN", searchKey: "LIKE", width: 300},
                {name: "ZH", searchKey: "LIKE", width: 300},
                {name: "EN", searchKey: "LIKE", width: 300},
                {name: "JP", searchKey: "LIKE", width: 300},
                {name: "KR", searchKey: "LIKE", width: 200},
                {name: "CreateDate", width: 170}
              ],
            },
            exportName: '场地管理-数据文件'
          },
        },
        item: {
          space: {
            // 以下参数默认由组件内部创建，但是如果有需要，开发人员可在这里提前设置，组件将按照您的预期渲染。
            // cache: false,
            // control: null,
            // dataExtend: null,
            data: {
              id: 1281,
              value: "",
              cellData: [
                {name: "TypeID"},
                {name: "Code"},
                {name: "CreateDate"},
                {name: "CN"},
                {name: "ZH"},
                {name: "EN"},
                {name: "JP"},
                {name: "KR"}
              ]
            },
          },
        },
        panel: {
          visible: false,
        },
        initCode: '',
        hasCode: false
      }
    },
    methods: {
      create: function () {
        this.item.space.data.value = null;
        this.item.space.control.load();
        this.initCode = ''

        this.panel.visible = true;
      },
      output: function (data) {
        // 导出以后，回调这里给客户端操作。
        // 核心思想是cellData包含search数据（不管有没有），回传以后发送export指令，后台api会直接返回导出的数据提供下载。
      },
      search: function (cell) {
        // console.log("search:" + JSON.stringify(cell));
      },
      validate(status) {
        if (status) {//默认校验通过
          if (this.hasCode) {
            this.octopus.tips.error('场地编号已存在，请重新输入');
            return false;
          }
        }
        return status;
      },
      change(data) {
        if (data.name == "Code") {
          if (this.initCode == data.defaultValue) {//编辑初始值 和 后来改变值相等；说明编辑时候没有改变Code字段的值，所以不用查询了
            this.hasCode = false;
            return
          }
          this.octopus.request.api.post("/Octopus/Engine/List", {
              id: 30,
              cellData: [{
                name: 'Name'
              },
                {
                  name: "Code",
                  filterKey: "=",
                  filterValue: data.defaultValue
                }
              ]
            },
            (data) => {
              if (data.status.code == 200 && data.content.queryData.length > 0) {
                this.hasCode = true;
              } else {
                this.hasCode = false; //取消
              }
            })
        }
      },
      modify: function (row) {
        // 加载对应的列关联绑定数据
        this.item.space.data.value = row.ID; //赋值
        this.item.space.control.load(); //刷新
        this.initCode = row.Code

        this.panel.visible = true; // 打开
      },
      view: function (row) {
        alert("view key: " + JSON.stringify(row)); //提示
      },
      del: function (row) {
        alert("del key: " + JSON.stringify(row)); // 提示
      },
      save: function () {
        // 刷新list
        this.list.space.control.load();
        this.back(); //返回
      },
      back: function () {
        this.panel.visible = false; //取消
      }
    },
    props: ["oData"]
  }
</script>
